
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       ModeFilter.h
  * @author     baiyang
  * @date       2023-7-27
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <stdint.h>
#include <stdbool.h>
/*-----------------------------------macro------------------------------------*/
#define MODE_FILTER_SIZE_5    5
#define MODE_FILTER_SIZE_6    6
#define MODE_FILTER_SIZE_7    7
/*----------------------------------typedef-----------------------------------*/
typedef struct ModeFilterFloat_Size5 *mmf5_t;

struct ModeFilterFloat_Size5 {
    uint8_t         _return_element;
    float           _output;

    bool            drop_high_sample; // switch to determine whether to drop the highest or lowest sample when new value arrives

    float           samples[MODE_FILTER_SIZE_5];       // buffer of samples
    uint8_t         sample_index;                      // pointer to the next empty slot in the buffer
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
/// constructor
void mff5_ctor(mmf5_t filter, uint8_t return_element);

// apply - Add a new raw value to the filter, retrieve the filtered result
float mff5_apply(mmf5_t filter, float sample);

// return the derivative value
static inline float mff5_get(mmf5_t filter) { return filter->_output; }

// reset - clear the filter
void mff5_reset(mmf5_t filter);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



